package com.nebula.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.nebula.common.Const;
import com.nebula.common.ServerResponse;
import com.nebula.pojo.Folders;
import com.nebula.pojo.User;
import com.nebula.service.IFileAndFolderListService;
import com.nebula.service.IFolderService;
import com.nebula.vo.FileAndFolderListVo;

@Controller
@RequestMapping("/fileAndFolder")
public class FileAndFolderListController {

	@Autowired
	private IFileAndFolderListService fileAndFolderListService;
	@Autowired
	private IFolderService folderService;
	
	@RequestMapping("/list")
	public ModelAndView fileAndFolderList(HttpServletRequest request,
			@RequestParam(value = "pageNum",defaultValue = Const.DefaultPage.pageNum)int pageNum,
			@RequestParam(value = "pageSize",defaultValue = Const.DefaultPage.pageSize)int pageSize,
			HttpSession session){
		User user = (User)session.getAttribute(Const.USERINFO);
		ModelAndView modelAndView = new ModelAndView();
		int parentId = 0;
		if(request.getParameter("parentId") == null || "".equals(request.getParameter("parentId"))){
			parentId = folderService.getAllFilesIdByUsername(user.getUsername());
		}else {
			parentId = Integer.parseInt(request.getParameter("parentId"));
			Folders folder = folderService.getFolderNameByFolderId(parentId);
			if(folder.getParentid() == 0){
				modelAndView.addObject("folderName","全部文件");
			}else{
				modelAndView.addObject("folderName",folder.getFolderName());
			}
		}
		List<FileAndFolderListVo> list = 
				fileAndFolderListService.getFileAndFolderList(pageNum, pageSize,user.getUsername(), parentId, request);
		modelAndView.addObject("list", list);
		modelAndView.setViewName("all-files");
		return modelAndView;
	}
	
	@RequestMapping("/search_list")
	public ModelAndView fileAndFolderSearchList(HttpServletRequest request,
			@RequestParam(value = "pageNum",defaultValue = Const.DefaultPage.pageNum)int pageNum,
			@RequestParam(value = "pageSize",defaultValue = Const.DefaultPage.pageSize)int pageSize,
			@RequestParam(value = "queries",defaultValue = "") String queries,
			HttpSession session){
		User user = (User)session.getAttribute(Const.USERINFO);
		ModelAndView modelAndView = new ModelAndView();
		int parentId = 0;
		if(request.getParameter("parentId") == null || "".equals(request.getParameter("parentId"))){
			parentId = folderService.getAllFilesIdByUsername(user.getUsername());
		}else {
			parentId = Integer.parseInt(request.getParameter("parentId"));
		}
		List<FileAndFolderListVo> list = 
				fileAndFolderListService.searchFileAndFolderList(pageNum,pageSize,user.getUsername(),queries,request);
		modelAndView.addObject("list", list);
		modelAndView.addObject("parentId", parentId);
		modelAndView.addObject("queries",queries);
		modelAndView.setViewName("search");
		return modelAndView;
	}
	
	@RequestMapping(value = "/all-list",method = RequestMethod.POST)
	@ResponseBody
	public ServerResponse allList(HttpSession session,@RequestParam(value = "parentId",defaultValue = "0") int parentId){
		User user = (User) session.getAttribute(Const.USERINFO);
		if(parentId == 0){
			parentId = folderService.getAllFilesIdByUsername(user.getUsername());
		}
		return fileAndFolderListService.getAllFileAndFolderList(user.getUsername(), parentId);
	}
	
	@RequestMapping(value = "/search-all-list",method = RequestMethod.POST)
	@ResponseBody
	public ServerResponse searchAllList(HttpSession session,@RequestParam(value = "queries",defaultValue = "") String queries){
		User user = (User) session.getAttribute(Const.USERINFO);
		return fileAndFolderListService.searchAllFileAndFolderList(user.getUsername(), queries);
	}
}
